package b;

import org.junit.Test;
import 笔试.A;

import java.util.Arrays;

public class _5搜索旋转排序数组_33 {
    @Test
    public void test() {
        int[] p = new int[]{4,5,6,7,0,1,2};
        System.out.println(search(p,0));
    }

    public int search(int[] nums, int target) {
        int l=0,r=nums.length-1;

        while(l<=r){
            int m=l+(r-l)/2;
            if(nums[m]==target)return m;

            //没找到时，不能简单l=m+1,因为不是整体有序的
            else{
                //当前在左段
                if(nums[l] <= nums[m]){
                    //target也在左段
                    if(nums[l]<=target&&target<=nums[m]) r=m-1;
                    //target在右段
                    else l=m+1;
                }
                //当前在右段
                else if(nums[l] > nums[m]){
                    //target也在右段
                    if(nums[m+1]<=target&&target<=nums[r]) l=m+1;
                    //target在左段
                    else r=m-1;
                }
            }
        }

        return -1;
    }


}
